{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Hopfield Network for Binocular Stereo" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This demo applies a Hopfield network (mean field theory) to binocular stereo" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# First initialize this notebook, load libraries for matrix manipulation and plotting\n", "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "def imshowPatch(im):\n", " plt.imshow(im, cmap='gray', interpolation='none')\n", " plt.axis('off')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Hand-wiring the constraints in a neural net." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How does one \"sculpt the energy landscape\"? One can use a form of Hebbian learning to dig holes (i.e. stable points or attractors) in the energy landscape indicating things to be remembered. Alternatively, one can study the nature of the problem to be solved and hand-wire the network to represent the constraints (i.e. reason out, or make an educated guess as to what the weights should be). \n", "\n", "We will follow the second approach to solve the correspondence problem. This problem crops up in a number of domains in pattern theory and recognition, and occurs whenever the features in two patterns need to be matched up, but one pattern is an unknown distortion of the other. Imagine, for example, the image of a face in memory, and you want to test whether an incoming stimulus is from the same person. Both patterns have more or less the same features, but they don't superimpose because they are from different views or expressions. So you want to try to morph one on to the other, but before you do that, you might want to decide which features go with which--i.e. pupils to pupils, nostrils to nostrils, etc.. Establishing correspondences between two similar but not quite identical patterns has also been a central, and challenging problem in both stereo vision and motion processing.\n", "\n", "In the next few sections, we will show how the weights in a network can be set up to represent constraints. Then we will look at three ways of letting the network evolve: asynchronous, synchronous, and partially asychronous updating. The first case exactly satisfies the assumptions required for Hopfield's energy function.\n", " \n", "This setup of the stereo problem is based on one of the earliest stereo algorithms from 1976. It is highly simplified." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Establishing correspondences: An example of constraint satisfaction" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Introduction to stereo and the correspondence problem" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you cross your eyes so that you can perceptually fuse the two random patterns below, you may be able to see a small square floating in front of the random background. Crossing your eyes means that the left image goes to the right eye, and the right to the left. (Some people are better at looking at the left image with the left eye, and right with the right eye. For this type of human, the images below should be exchanged.)\n", "This is an example of a random dot stereogram originally developed by Bela Julesz in the 1960's." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is an example of a random dot stereogram originally developed by Bela Julesz in the 1960's." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "image/png": [ "iVBORw0KGgoAAAANSUhEUgAAAWwAAAC3CAYAAAA2EWThAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\n", "AAALEgAACxIB0t1+/AAAEapJREFUeJztnT12HEcShGNGor8GryNAgngKHoQeRfxYPAkuQWkAyV9j\n", "L7DWWjwCRcw6MoCsmIeYGvQMU/o+D/26a6qrg8l+GZ1Zq+12KwAA+PZZn3oCAACQQcAGAGgCARsA\n", "oAkEbACAJhCwAQCaQMAGAGgCARsAoAkEbACAJhCwAQCa8P1SA2+324+S3j0+9t133w3nPTw8DMeu\n", "rq6e/H13dxf95v39/bPj17El6ccffxyOud9M5uHGury8HI799NNPw7Hffvvtyd/uftxY7jd/+eWX\n", "J39fXFwM57jxP3z48Oz4Z2dnwznu2SZjpc/WjHUr6W108Quz3W4/S3r9+Fiibae9WW0n/26kTNvp\n", "HBJtJ7qWMm0nupYybSdalDJtp2Ml6+rGkrTadT5v2AAATSBgAwA0gYANANCE1VLd+tIctsvh1NzP\n", "ajWmdNx17l6ur6+fm6p+/fXX4Zibq2NHDupZXC7u999/f/K3y4G5POXXr1+HY/W+3Vgu73Z+fv7s\n", "WO6c2Xml19W1kXR7fn7+TeewqzbcM5jVdqJraV7bS+payrSd6GfXWFXbia53nTc7r0Tbbm3Oz8/J\n", "YQMAdIeADQDQBAI2AEATCNgAAE1YzHRcr9eD6eh+yxlfFWcqrNfj/zXuA/16rTNFHM6YcWZDxZkP\n", "qRFTSU3HpIjCmSmu4CDBrUNaFFVJ9WfGv314eDiJ6bherwfT8djadtfNajvRtZRpO9G1lGk7LQ46\n", "trYTXUuZtnf8u8F0BADoDgEbAKAJBGwAgCYQsAEAmrBYt760GsgZBtWQcJ29UoOl4kyRA6rthvm7\n", "ed3c3Dx7nTTeZ2Ja7aLO4+eff47mkODWIa08rWaQG8uZx6kxdgxmte2Mtm9B24mud82rajvRtTSv\n", "bTeHY2s70fWusaq299U1b9gAAE0gYAMANIGADQDQBAI2AEATFjMdXdtIR9L2MG316Lb3qVsWOQPE\n", "kc4/qahKtzWqrTCdYeQMj2RrKHdd2u4zMc/cHNx91/G/fPkynPP996MsnQZmW4Aeyqy2nTE5q223\n", "FddLajutFEy2onMtXhNtp1ueJdpOdO2uc/NIdC1l2t5X17xhAwA0gYANANAEAjYAQBMW69Z3fX09\n", "dOtzOWaX86q5pTQ3t9lshmP1/g7p7JUUObg8ZboVV82DJTl5d52UfZCfrL003vchz6Pek5u7y12a\n", "vN6tpJN067u+vh669SXaTotKHHUt3b/bWW2nxTuJthNdS5m2Z3Utza998jwSXUuZtnfkq+nWBwDQ\n", "HQI2AEATCNgAAE0gYAMANGEx03G73Q6mo9sOx5kBScFB2jGtmgiuE5zb8ict3qikHdmSbmLuOjeH\n", "xFBJjZ/EiHGFEE5HrotaHevy8nI45/3798Mx82xvP3z4cBLTcbvdDqZjou2kSEzKtO2e06y20+Kd\n", "RNtpl7xE26lRmGg7NRgTbSe6ljJt7yimwnQEAOgOARsAoAkEbACAJhCwAQCasJjpeHV1NZiOjmQ7\n", "JJfQd9vvJNVT6/X4f5QzedJ1qaaBMxocSfcwd9/p1mWV2c580rgWbl6zZqtbLzeW4WSm49XV1WA6\n", "OpbUtqt4fUltOzMs0XaqqUTbia6lTNuJrnfNK9my0I0/q21MRwCAvwEEbACAJhCwAQCaQMAGAGjC\n", "Yqbjer0eTMelt3RybUwToyQ1B5LqstTIcG0vkxaa7h7d+LWNozNF0ueRVLO5ubpjtcou3fLJcLvZ\n", "bE5iOq7X68F0PLa2U3M70XZaNZloO9H1rrHqPSa6lua1nZqHda6JrqV5bW82G0xHAIDuELABAJpA\n", "wAYAaMJiOey7u7shh72jM5W79snfrgDBdUdzH+3X7lsuB5bmstx5Lj+XjOVy5HV90jyiyxvWubp5\n", "uvtxnfJcd7KKm2vSwczl+ZKCE0m3q9XqJDnsu7u7IYedaNs981ltu65ys9qe1bUbK9G1lGk70bWU\n", "aXtW19I417Qz36y2Vy6Q/QVv2AAATSBgAwA0gYANANAEAjYAQBMWMx0vLi4G09El3F1HrmoQuI/S\n", "nenizL2av3emiDM3nCniTJ06/7Q7mjOWKocYhfWenI/h7tFR7zs0BaN7dMZMuM3Uybr1XVxcDKZj\n", "om33nGa17Z5nqu1kK65E69Kod6d110XQHat6dPfj1vDNmzfPjuVw95hoO9G1NK9tuvUBAPwNIGAD\n", "ADSBgA0A0AQCNgBAExYzHbfb7WA6usoiZ7BUIyY1GF1FVTUW0q5q7rykytBVPKWVjsl2ZqlRWA3L\n", "dMsnN9fEKEkr0OrzSLsPGm4fHh5OYjput9vBdEy0fYh5Xp+fM8wO0XYlraitGprVujTq/ZBK32Re\n", "obkdVQ275zGr7YeHB0xHAIDuELABAJpAwAYAaAIBGwCgCaML8kK4qihnsLjzzs7Onvydtmd0VGPB\n", "GQ3OtEirlOp5znxI55q00EwrHaup4yow3donpkjaetaZW/Ue07HS6spjMKvtqmtpXtupiZZUOqbt\n", "hd2/iar3Wa1L4xqmOki0HRrZkR4TXadj7atr3rABAJpAwAYAaAIBGwCgCYvlsA/5sD8pOHAf3qd5\n", "1FlcPrDmqdxH9a4AxuUD61zTvJg7r+bGXIex2W5ibh1cDtf9Zr1Hl8N1Y6Xbyx2DWW0nRWJSpu30\n", "3pNCEzdWonVp1Pus1qXM30g7X9bfTItkEm0nupYybe+ra96wAQCaQMAGAGgCARsAoAkEbACAJixm\n", "OqYFEe68uuWPS8K7D87d+DWp78ZyH/u7TnZu/GoiuM5kyXUOZ0g4nAlWx0+KHnZRTbC0cMAVNFSc\n", "geM63zmz6VSm46y23VZWs9pOzSpngldjza1tqtmq91mtS5ne08K0RNvO3E20nehayrS9r655wwYA\n", "aAIBGwCgCQRsAIAmELABAJpw1EpHl6x3BlM1a5wJ4yrEki2F3LzSajtnUiTbmbnfdCZdNXBSU9Dd\n", "dzWW0g5z7jdnOx4mnducweLGT9fiGMxq293XrLadWZVqu87VGYWJ1qVRQ7Na33VtxWkq2Z4t0fWu\n", "Y1XbaUfCRNv76po3bACAJhCwAQCaQMAGAGgCARsAoAkrZ168yMCr1UdJ7x4fSyuL6lZKzlRwZo0z\n", "N2qlV2LC7DrPUU0Ed11aGZe0XnT37QzFxGxNt02qz8hVTabVcvVY2nL0zz//rIduV6vV23G2y7Na\n", "rT5Lev34WKJtt0XYrLZdBWOq7TpWanw5HdffnNW6GyvVelqVXHGxJ9F2omsp07bRtVYukP0Fb9gA\n", "AE0gYAMANIGADQDQhMUKZxzpx/6VdFssR7LtluMltxtzuTiXu6xzS4sqXB6sjp/mit1v1meUro3L\n", "B9bxXU7S3c+3tEWY49jaTjsmumOzzzNhVuvuWqdPl5NPtJ3oWsrWItG1lGmbLcIAAP6mELABAJpA\n", "wAYAaAIBGwCgCYuZju7DfpfQdwn8H3744cnfznxw1zkSAyc18pxBUO/p06dPwzluqyBnLCRdwdy8\n", "nLlRx1+vx/+bnVHi7rH+pluvpKhCGk0jd13aye1UpuOstquupXltp+a5e+513ZIiFsnfY9X7rNal\n", "Ue9O6+k2g1Xbia6lTNtpgVKi7X11zRs2AEATCNgAAE0gYAMANIGADQDQhMVMR2ecOKMkMRGSjmOS\n", "rzarpoublzNdnJGXVDM5kyc1N+rc3NqknckS48qtlzNKbm5unh3L/d779++HY9WUcs82NZZOxay2\n", "3T3MatvpzM3LGdd1/Fmtu3nMal0a1+sQrdf1mtW1Gz/RtZRpe19d84YNANAEAjYAQBMI2AAATSBg\n", "AwA0YTHTMd12yFG3W3JmhzNTkqqhdHsrV4nlzMnaOtIZDX/88cez85JGAyKtgnLn1Wq8ZPu0lNQo\n", "cabOw8PDk7+TSjzJVz+eilltu23EZrU9u72VNGo7bb2bmGjOFHT3k27BV3H347SxpLYTXUuZtvfV\n", "NW/YAABNIGADADSBgA0A0ISjbhGWbh9UP5g/OzsbznE5MJcPqjkvl390eSuXK3M5QlcokIzl5lHv\n", "O+1G53KQ9UN+d49uXd3zqPNICy2SLmqO2fzmKUm07QpBZrWdairR9qyu3VjuHt2xRNuJrqVM24mu\n", "pUzbs7qWDtc2b9gAAE0gYAMANIGADQDQBAI2AEATjmo6usS/M11qgUFqvrnzamctVxCQmhuumCaZ\n", "mytacddVAyf9qN4VK7gP+SupcVtxposzvJIiCjcHN366JdypSLTtCmdmte06xs1qe1bX0qjtRNdS\n", "prNZXUujrtJ/S4m2DykOquPvq2vesAEAmkDABgBoAgEbAKAJBGwAgCYcdYuwdNuheq0zRVznMGcU\n", "VhPkkC2MPn36NBx79erVk7+d+eBw5ka9T3ePzshIOrI5o8QZRG59nKGWkGzZ5uZwiAl2DGa17a6b\n", "1Xb67BJtJ7qWMm0nupYybaddNBNtH1vXbg7SOP99dc0bNgBAEwjYAABNIGADADSBgA0A0ITFTMe0\n", "baQzDGp1lkvMu216nLGQmFzpllfOiKlzdWO5KqvEWHOGrFsv95v1PGcYzRo47jm6irrZSkdn4Lk1\n", "PJUROattt0az2k5NrkTbia53jVWfS2oYJ9pOdC1l2k5N90Tbh1Q6Vm3vq2vesAEAmkDABgBoAgEb\n", "AKAJBGwAgCasXNXOS3B/f/9R0rvHx9LKpc1m8+TvdK86l6xP2iomc5C82VRJjVW3FtW4cFVqzlh1\n", "Bk4d391japC69rCV2bVPzSAz1u1ms3n77A8swP39/WdJrx8fS7TtNDWr7bRdaKLtRNdSpu1E11Km\n", "7UTXUqbtWV1L82ufaNuNtdlsdk6MN2wAgCYQsAEAmkDABgBowmKFMy43l37YX4+l2w65YoI6Vlp4\n", "4nJL6bUVN1fXua12D3O5RddhzBU51PPS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kDABgBoAgEbAKAJBGwAgCYQsAEAmkDABgBoAgEbAKAJBGwAgCYQsAEAmkDABgBoAgEbAKAJBGwA\n", "gCYQsAEAmkDABgBoAgEbAKAJBGwAgCYQsAEAmkDABgBowv8BGbQ5ifQnO/0AAAAASUVORK5CYII=\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Generate random dot stereogram, 32 x 32\n", "# Make a randome image, the value of pixel is [0,4]\n", "left = np.random.randint(0, 5, size=(32,32)) / 4.0\n", "\n", "# Shift a patch two pixels to right\n", "xoffset = 1;\n", "r = [10, 10, 10, 10] # [x, y, w, h]\n", "patch = left[r[1]:r[1]+r[3], r[0]:r[0]+r[2]]\n", "\n", "right = left.copy()\n", "r[0] = r[0] + xoffset; \n", "right[r[1]:r[1]+r[3], r[0]:r[0]+r[2]] = patch # Shift the patch\n", "right[r[1]:r[1]+r[3], r[0]-xoffset:r[0]] = np.random.randint(0, 5, size=(r[3], xoffset)) / 4.0 \n", "# Fill the hole with random value\n", "\n", "plt.subplot(121); imshowPatch(left)\n", "plt.subplot(122); imshowPatch(right)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is made by taking a small square sub-patch in one image, shifting it by a pixel or two, to make the second image. Since the subpatch is shifted, it leaves a sub-column of pixels unspecified. These get assigned new random values. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The stereogram mimics what happens in the real world when you look at an object that stands out in depth from a background--the left eye's view is slightly different than the right eye's view. The pixels for an object in front are shifted with respect to the background pixels in one eye's view compared to the other. There is a disparity between the two eyes. The distances between two points in the left eye and the distance of the images of the same two points in the right eye are, in general different, and depend on the relative depth of the two points in the world." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To see depth in a random dot stereogram, the human visual system effectively solves a correspondence problem. The fundamental problem is to figure out which of the pixels in the left eye belong to which ones in the right. This is a non-trivial computational problem when so may of the features (i.e. the pixel intensities) look the same--there is considerable potential for false matches. A small minority don't have matching pairs (i.e. the ones that got filled in the vertical slot left after shifting the sub-patch). We'll get to this in a moment." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Human perception solves the stereo correspondence, so let us see if we can devise a neural network style algorithm to solve it. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Display a pair of images" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is not easy to fuse the left and right image without a stereo device (it requires placing the images side by side and crossing your eyes. We can check out our images another way." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The visual system also solves a correspondence problem over time. We can illustrate this using animation. When the animation begins you can find the central patch almost magically appears to oscillate and float above the background. When the animation stops, the central square patch disappears again into the camouflage." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Animation: Execute below cell to see animation" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "image/png": [ "iVBORw0KGgoAAAANSUhEUgAAAPwAAAD9CAYAAACY9xrCAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\n", "AAALEgAACxIB0t1+/AAAFQlJREFUeJzt3V+MnNV5BvDnWeOoJVT1olS2RS3tXqRVLlLZjUQvwGYN\n", "TmRuCNwkQoqEXBTlogXUSi2kkmHBFyVIIKRecINBhlREURCIqIqCDV5CVEFLugvmXwjSrgXEXqeK\n", "3YKqqjb79mK+3Y7Xe55ZvzvfzNLz/KSVd+ebme98Z77X8+eZcw4jAmZWh5FhN8DMBscFb1YRF7xZ\n", "RVzwZhVxwZtVxAVvVpF0wZPcS/Jdkr8ieWc/G2Vm7WAmhye5AcAvAewB8BGAfwVwc0S809/mmVk/\n", "XZK83ZUA3o+IOQAg+QMAXwewVPAk/Y0esyGKCC6/LFvwVwD4oOvvDwH82fIrLSwsLP0+OTmJyclJ\n", "AMCGDRuKd9x9m+Xuvffe4rapqanituVmZ2cxPj4OAHjppZf63paJiYnituXtPHr0KHbv3r3ittVS\n", "+1vs85Us7ndRd7+8+OKLxdupPlP7U+28++67z7uP7vu59tprU2255557Um3ZtWtXsS3q3M3ub7WP\n", "e/e50mt/5AW1DiD/Ht7P3mafQdln+I8AbOv6exs6z/Ln6f6fcW5uLrkrM+tlampqVa8UsgX/GoAv\n", "khwD8GsA3wRw8/IrdRd89uVqGzZt2jTsJiwZGxsbdhOWrJd+US9/B209tUWdKxMTE+e1tfSWM1Xw\n", "EXGO5F8C+CmADQAO9vqEfj113Ojo6LCbsGTxPfN6sF76ZT2dK+upLf04V7LP8IiInwD4yZpbYGYD\n", "k8rhV3XHZJQ+5c5+0qneFpQ+lex1n+r477vvvuI25YUXXihuU8euqGPI6v5kfLmf/exnxW3qcVDp\n", "xaefflrcpvpa7U89A19zzTWp/anbtXF82ftUj9HExMSKsZy/WmtWERe8WUVc8GYVccGbVcQFb1YR\n", "F7xZRVqN5UpRmdpn9osOKroZGSn/v6YGiaj7VJGWomI5FcEoKvLJRm9KNpbLDkZSMZkaWJOlHofs\n", "wC8lW4OqLRHhWM6sdi54s4q44M0q4oI3q4gL3qwiLniziqSHx65GKd7IjlRScZCa2ywboSkq0mpj\n", "9JPqF3V8Bw4cSN2n6s82xoirY7juuuuK29QxZKnHITvyUsWHan8qNlbnUimK9jO8WUVc8GYVccGb\n", "VcQFb1YRF7xZRVr9lF7NM1eSnYcsO+db9yojyy1fmaWb+hRbyfQJkB8koj79Vvep5uVTKYT6pDq7\n", "Wo+6z+w8h9k0SB2D6mvVlrNnzxa3XXJJuUQz57yf4c0q4oI3q4gL3qwiLnizirjgzSqypk/pSc4B\n", "+E8AnwI4GxFX9qNRZtaONc1pR3IWwFci4rcrbItShKGiMBUVZQd7KEePHi1uU33Txjxr2QFAKq7M\n", "LsWkYqRslKnuMzufXxvnSxvnkuoz1S8qBlSxHMnW5rTLBctmNnBrLfgAcITkayS/3Y8GmVl71vpN\n", "u6si4gTJPwBwmOS7EfFyPxpmZv23poKPiBPNv78h+QyAKwEsFXz3e5qxsbG+LGhvZheampqSXwle\n", "lC54kpcC2BARH5P8PICvATjvEwb1AY6Z9c/ExMR5H9CWPuxby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f8esv8roqIHQCuB/AXJHcOu0GLovMabZj99QiAcQDbAZwA8OAgd07yMgBP\n", "A7gjIj7u3jbovmna8qOmLZ9gSH0TEQvRmSL+DwHsWmk+SVxkv7Rd8B8B2Nb19zZ0nuWHIiJONP/+\n", "BsAz6LzlGKZVzQ04CBFxKhoAHsUA+4bkRnSK/cmIWJwubSh909WW7y+2ZZh90+z/oueTLGm74F8D\n", "8EWSYyQ/B+Cb6MyHN3AkLyX5e83vnwfwNQDH9K1at6q5AQehOXkW3YQB9Q07s00cBPB2RDzctWng\n", "fVNqyzD6Zq3zSRYN4JPG69H5tPN9AN8d9CedXe0YRyclmAHw5qDbAuApAL8G8D/ofK6xD8DlAI4A\n", "eA/A8wA2Daktfw7gCQBvAHi9OYk2D6gtVwNYaB6X6eZn7zD6ptCW64fRNwC+DODfmra8AeBvmsvX\n", "1C/+aq1ZRfxNO7OKuODNKuKCN6uIC96sIi54s4q44M0q4oI3q4gL3qwi/wuVvctPG7yBxwAAAABJ\n", "RU5ErkJggg==\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Alternating the two images to show it's relly moving\n", "import time\n", "from IPython.display import display, clear_output\n", "f, ax = plt.subplots()\n", "t = 0.8\n", "for i in range(8):\n", " clear_output(wait=True); ax.imshow(left, cmap='gray', interpolation='none')\n", " display(f); time.sleep(t)\n", " clear_output(wait=True); ax.imshow(right, cmap='gray', interpolation='none')\n", " display(f); time.sleep(t)\n", "clear_output()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Two-state neural network implementation using Marr and Poggio (1976) constraints" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Simplify the problem to one dimension" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will apply the constraints proposed by Marr and Poggio (1976) to try to solve the correspondence problem for just the middle rows of left and right images:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "image/png": [ "iVBORw0KGgoAAAANSUhEUgAAAXcAAAA2CAYAAAA8s9mDAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\n", "AAALEgAACxIB0t1+/AAAAZ9JREFUeJzt3UtOQkEQhtFqcV0898VrXxDYgEviOnJoUjfBgH/OGVf6\n", "Ni1+ISYtY5qmAiDLx6s3AMDziTtAIHEHCCTuAIHEHSCQuAMEEneAQOIOEEjcAQKJO0AgcQcI9Pnq\n", "DfwYY7T+yc1ms2mveTgcWnPL5bI1t9vtnv7sOW63W2vudDq113w8Hk9fs2u9Xrdnj8fj09e83++t\n", "uev12pqbc0bd90d3j3N0z7Kq/54bY7TX7L72OT/Lru7rqeqf/eVyac0tFov2s7u/l1X168H75A4Q\n", "SNwBAok7QCBxBwgk7gCBxB0gkLgDBBJ3gEDiDhDobW6o8t72+3179nw+t+bm3L7s3mrsPruqarVa\n", "tea2221rrntTsaq/zznn/he3Wfm/fHIHCCTuAIHEHSCQuAMEEneAQOIOEEjcAQKJO0AgcQcI9E43\n", "VL9evQGAFGOaWt9LDcA/4s8yAIHEHSCQuAMEEneAQOIOEEjcAQKJO0AgcQcIJO4AgcQdIJC4AwQS\n", "d4BA4g4QSNwBAok7QCBxBwgk7gCBxB0gkLgDBBJ3gEDfEyBDfE5+hU4AAAAASUVORK5CYII=\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "leftMiddle = np.matrix(left[16,])\n", "rightMiddle = np.matrix(right[16,])\n", "imshowPatch(np.concatenate((leftMiddle, rightMiddle)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We've displayed the left eye's view above the right eye's, so you can try to spot which pixels have been shifted. Because the patch was shifted horizontally, we haven't lost the essence of the correspondence problem by reducing it to one dimension. You can see the ambiguity in the correspondence problem." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Following Marr and Poggio, we will try to solve the correspondence (i.e. which pairs of pixels in the two images belong together) using three constraints: \n", "\n", "- compatibility\n", "- uniqueness\n", "- smoothness\n", "\n", "We will see what these constraints mean as we move along. The challenge is to design a network that enforces these constraints." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The compatibility constraint" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The compatibility constraint says that similar features in each eye should match, in other words like pixels (same graylevel) in each image should match. We can represent compatibility by constructing a \"compatibility matrix\" which has a 1 at each location where there is a possible match (i.e. in graylevel color), and zeros elsewhere." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's the plot of the compatibility of the middle row for the left and right eye's stereo pictures, where white represents 1 (compatible), and black 0 (not compatible). " ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "image/png": [ "iVBORw0KGgoAAAANSUhEUgAAAPwAAAD9CAYAAACY9xrCAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\n", "AAALEgAACxIB0t1+/AAAB4BJREFUeJzt3c11K0UQBlBZtlMkFHIgFnIhBVZE4R+xZeEpoKxq9fi7\n", "d6ujsXpGn/sd16vqp9vtdgEyXB/9AYB1BB6CCDwEEXgIIvAQROAhiMBDEIGHIC9TF77dbr9dLpdf\n", "v3rtej3H75mnp6fD1z4+Pg5fO8v6zqJ6Dp+fn4evVf+pbKdnNPA9+/12u/3y5Xv+zwcDzk3gIYjA\n", "QxCBhyACD0EEHoKMleVeXo4vXZUanp+fD1+ryhfv7++ta1aqsk73ml3dtVfPYeKedXXXd5byWvde\n", "3/s57HO3gHECD0EEHoIIPAQReAgi8BBkrCxXdTG9vr4evtYt2VXX7HZUVT9vYrz3ROmt+pzV+7q6\n", "nV/dz9l9DhOfs/q+dO/1vb9ndngIIvAQROAhiMBDEIGHIAIPQcbKcpXVJbuJzq+JzqjVpalKda9X\n", "d+BNrG+nTsjuva7ed8QOD0EEHoIIPAQReAgi8BBk7K/0E3/FnvgLftfqppTKxF/UJxpyqvd1m1lW\n", "P9vK6uanau1HlTA7PAQReAgi8BBE4CGIwEMQgYcgY/WjqpzwE2baVc5S1lndkNN9DmeZvdedade9\n", "15332eE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qfDHRodbtfuquoTscsjIxGHN1\n", "p2DX6s+pWw4YIfAQROAhiMBDEIGHIAIPQcbKcqvPiDtLl1b3fRNnk03cs6qk1R0O2S1JVibKazt1\n", "eh6xw0MQgYcgAg9BBB6CCDwEEXgIMlaW65befvq5c11Vuau79olz4CrVZ9mpA6/7falMDLjsfM/s\n", "8BBE4CGIwEMQgYcgAg9BBB6CPKRbbqLsMXHNSrW+1Sa683bSHfy5k+4a7r0+OzwEEXgIIvAQROAh\n", "iMBDEIGHIGNludWDI7smOpVWn3O3eg2rOwWr+3K9Hu9ZE4MxJ+7LynP17PAQROAhiMBDEIGHIAIP\n", "QQQegoyV5brdQTuVwiYGY64+X607qLJ7X1aXJHcajDkxFPTeHY12eAgi8BBE4CGIwEMQgYcgAg9B\n", "1h4u9h9MDCXsXnOnYZtnWcPKgYzfMTEY8wxDQe3wEETgIYjAQxCBhyACD0EEHoI8pCw3MbTvLIMO\n", "k4dYVs9hQncNq78vK9nhIYjAQxCBhyACD0EEHoIIPAQZK8udpVQ0MZRw5Vlhl0u99mpY406DMSfK\n", "XRMDSruDMXcp2dnhIYjAQxCBhyACD0EEHoIIPAR5yNly3bPJqvLT6nO9JkqEXd0SU7eMVP281SW0\n", "romBk9WZdKvv9RE7PAQReAgi8BBE4CGIwEMQgYcgY2W5ie6gbvmpa6cSYaW79qqM1L3Xq9derWF1\n", "12KlW7LrlniPfp4dHoIIPAQReAgi8BBE4CGIwEOQsRpKtzyzUxdat9Ns9efsmig7Tqx9p/JaV3Wv\n", "397eDl/rdtkdscNDEIGHIAIPQQQeggg8BJnsdPjrcrn8MXh94Gt/Hr3wNNFlBuzJP+khiMBDEIGH\n", "IAIPQQQeggg8BBF4CCLwEETgIYjAQxCBhyACD0EEHoIIPAQReAgi8BBE4CGIwEMQgYcgAg9B/gbe\n", "n9lLpZjqAgAAAABJRU5ErkJggg==\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "leftRepeat = np.repeat(leftMiddle, 32, axis=0)\n", "rightRepeat = np.repeat(rightMiddle.T, 32, axis=1)\n", "compatibility = (leftRepeat == rightRepeat)\n", "imshowPatch(compatibility)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The uniqueness and smoothness constraints" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But many of these possible matches seem unlikely. A given point in one eye shouldn't have lots of matches in the other eye--a point on a surface in the world typically maps to at most one point in each eye. We would like to find a line through the compatibility matrix to indicate unique matches. So we have to discourage more than one unit from being on in any given row or column (i.e. enforce a uniqueness constraint)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Also when we think about how features along surfaces typically change as a function of position, we realize that surfaces to a first approximation are smooth--thus, nearby points on a surface project to nearby points in the two eyes. For convenience, let's assume an even stronger constraint in which nearby points have constant disparities. (Recall that disparity refers to the relative shift of corresponding points in the two images.)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This means to we should encourage elements that have nearest neighbor support along the diagonals to be on (to encourage regions that have constant disparity). This latter constraint is called a \"surface smoothness constraint\", which refers to the underlying assumption that changes in depth usually change gradually--sudden changes in disparity are rare. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will follow Marr and Poggio and set up a threshold logic unit at each location of the compatibility matrix, and wire each up to reflect the above constraints. We are going to have to worry about the boundaries. There are several ways of doing this. One is to have a \"free\" boundary in which the connection weights at the boundaries are actually different (to compensate for a lack of neighbors in the other directions). Although not biologically realistic, a second way is to use a toroidal geometry, restricting indices by the following modulus function: myMod[x_] := Mod[x-1,size]+1. This option is computationally convenient and makes it possible to comply with the restriction of symmetric connections everywhere. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To encourage uniqueness, let's make the vertical and horizontal connection weights all equal and negative with a weight inhib. To encourage smoothness, we'll make the diagonal support positive with mutally excitatory weights excit. The network will have biases for each unit (which are equivalent to appropriate thresholds for each unit) proportional to the original compatibility matrix. These biases correspond to the Ui's in the Hopfield net and serve to prevent the network from loosing this strong constraint from the data as the iterations progress. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that we won't set up the network calculations using a matrix and matrix multiplications as you will do in the Hopfield memory examples. This is because most of our connections are zero and we don't want to waste time multiplying zero elements and adding them up. So the code below will look messier." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Hopfield Net: Asynchronous updating--all sites visited randomly, at random times\n", "\n", "(Animation)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To do random asynchronous updating, you simply pick a site at random and update it, and then pick another, and so on." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [], "source": [ "excitWeight = 2\n", "inhibWeight = -1\n", "theta = 13\n", "k = 8\n", "V = compatibility.copy()\n", "\n", "def r(ind):\n", " return ind % 32\n", "\n", "for iter in range(10000):\n", " p = np.random.randint(32, size=(1,2))\n", " x0 = p[0,0]; y0 = p[0,1]\n", " # sum over the same row\n", " inhib = 0\n", " for x in range(-4, 5):\n", " inhib = inhib + V[y0, r(x0+x)]\n", " \n", " # sum over the same col\n", " for y in range(-4, 5):\n", " inhib = inhib + V[r(y0+y), x0]\n", " inhib = inhib - 2 * V[y0, x0]\n", " \n", " # sum over diagonal\n", " excit = 0\n", " for off in range(-4, 5):\n", " excit = excit + V[r(y0+off), r(x0+off)]\n", "\n", " excit = excit - V[y0,x0]\n", "\n", " V[y0, x0] = (inhibWeight * inhib + excitWeight * excit + k * V[y0,x0] > theta)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "image/png": [ "iVBORw0KGgoAAAANSUhEUgAAAWwAAAC3CAYAAAA2EWThAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\n", "AAALEgAACxIB0t1+/AAACHhJREFUeJzt3UFyHLkRBVCQEvde+HRzkLmDT+LbeOELeOWVjyCJ8saO\n", "mECDVhooVNVnv7fs6OpGFXv+IJBK4OXnz58NgPt7vXoAANQIbIAQAhsghMAGCCGwAUIIbIAQAhsg\n", "hMAGCCGwAUJ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Q291ZV/2sfhzVLsDdZp/F7LNf\n", "eR+/Vtni1has92SGDRBCYAOEENgAIU5dw55trhhdNzr6aLYJp9qoURnH6Ptmj+KqNqhUnsXKcWB9\n", "Q89sUxH30P+tHC2WwwwbIITABgghsAFCCGyAENuKjtUC42invCP/Yf9sw8XoutFrfcFmVKwZ3WOl\n", "qFMde6U4uVJE6ouMK8WnSpGWc42KyAqR92SGDRBCYAOEENgAIQQ2QIhtRcdKx9xH75u9brYT8chO\n", "ylGxZnextVIgHX3fyO4jwqrj4FqzhUhFyL3MsAFCCGyAEAIbIITABghxeadjpYC1UnyrFLlWtgbt\n", "x/r29vbwnmohsv/OlQLdHZ5h5b4VqXJUCpG6IfcywwYIIbABQghsgBCXN85U1rpX1nL7caysrVdU\n", "m3wqTQizu/yNvvPIZ1g1u+ZZve/RMW6cq/8b2+VvLzNsgBACGyCEwAYIIbABQmwrOo5Ui1d90al6\n", "3exOc6NCyahgNttgUy269OM4cre73c9wtphbLdIqUmVw3NheZtgAIQQ2QAiBDRBCYAOEOLXoODJb\n", "5BqpdFeOvm+0w95sAWSlw7Mfx+wufyuO3K1v9hg0nY6fi+PGjmOGDRBCYAOEENgAIQQ2QIhTi47V\n", "brhKoXBUhJrtkKt221XGUT12qzKO6rasla7MIws4R3aB6nR8TpVC5JG/s8/CDBsghMAGCCGwAUJs\n", "W8OeXa8euaJZpPq+2SOvZu9ptK5X2T1vZXe02fXw3U1RfC79b7u6K+Qz7fxnhg0QQmADhBDYACEE\n", "NkCIbUXHaoGxUkyava612u5wKwWKvlBSHevsUV8jo8/vXzvyGLSVAmClKWp3kZkMld91a8+1858Z\n", "NkAIgQ0QQmADhBDYACFO7XScLVYd2eU2GteRVromd6ruPnj2OEZjuGJc5Krs/PdZuiHNsAFCCGyA\n", "EAIbIITABgixreh4xYL+7HaeRx5BttK5t7MrczT29/f3X46htVrX2Ox9P/uRT+zRFyIr3ZCt3f+3\n", "Z4YNEEJgA4QQ2AAhBDZAiG1Fx2pnXaUwtVIU7LfzrHYYzhYnR9dVt1Ltxza6bvZZjMY1u31ldVvW\n", "2S1knd/I0SrdkK3dvxBphg0QQmADhBDYACG2rWFXze7MVr1u9w6BlXEcOYYrdrer7LBXuQ7uZHZd\n", "+8o1bTNsgBACGyCEwAYIIbABQpxadJzdme2KneaSdusbPYtZlfHPPvvW7tWEAL27Hzdmhg0QQmAD\n", "hBDYACEENkCIbUXHK4pvszvGHXlM1Wiss7uCze7y19rjM6wWSmZ3PKzs8jcahyIkd3en48bMsAFC\n", "CGyAEAIbIITABghx6hFh1SOv+sLd7FFTo3GsFEMrRmOdLciNPmu2eHjkM6ya7Rqr3veRHZ5QdeVx\n", "Y2bYACEENkAIgQ0QYtsa9sr6TaVRo6rShLOyvturjnW0Dla57yPHOhrDkU1Ele8crf0dWVOAM5x1\n", "3JgZNkAIgQ0QQmADhBDYACG2FR2rxbFKI8tKwakvot2lyDXbMDQ71t3FxKr+vt/e3h7eU21C0DjD\n", "nVUKkaP3/K/ftRk2QAiBDRBCYAOE2LaG3Vr7Z2vtbxs/n+f2jwu/+++ttT9d+P08qZdR8QuA+7Ek\n", "AhBCYAOEENgAIQQ2QAiBDRBCYAOEENgAIQQ2QAiBDRBCYAOEENgAIQQ2QAiBDRBCYAOEENgAIQQ2\n", "QAiBDRBCYAOEENgAIQQ2QIh/A+aKGc0vENlxAAAAAElFTkSuQmCC\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "plt.subplot(121); imshowPatch(-compatibility)\n", "plt.subplot(122); imshowPatch(-V)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Energy and constraint satisfaction" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What use is energy?\n", "\n", "We haven't computed the Hopfield energy for each iteration. But we could, and in fact this calculation can be useful. \n", "\n", "In our toy example of a random dot stereogram, we know the answer (we made the stereogram), so computation of the energy of the final stable state can be compared with the energy of the right answer. If the right answer has a lower energy than the stable state reached, we know we got stuck in a local minimum. If the right answer has a higher energy, then the constraints must have been set up incorrectly. Energy can be a useful number to calculate during network model development." ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.8" } }, "nbformat": 4, "nbformat_minor": 0 }